package Clases;

import java.io.IOException;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListadoPromedio.java
 *
 * */
public class ListadoPromedio extends javax.swing.JFrame {

    /** Creates new form ListadoPromedio */
    public ListadoPromedio() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        combo = new javax.swing.JComboBox();
        btnAceptar = new javax.swing.JButton();
        btnPromedi = new javax.swing.JButton();
        txtPromedio = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaLista = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        getContentPane().setLayout(null);

        combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cedula", "Nota final", "Aprobados", "Reprobados" }));
        combo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboActionPerformed(evt);
            }
        });
        getContentPane().add(combo);
        combo.setBounds(20, 10, 180, 20);

        btnAceptar.setBackground(new java.awt.Color(0, 204, 204));
        btnAceptar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAceptar);
        btnAceptar.setBounds(240, 10, 120, 40);

        btnPromedi.setBackground(new java.awt.Color(0, 204, 204));
        btnPromedi.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnPromedi.setText("Promedio");
        btnPromedi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPromediActionPerformed(evt);
            }
        });
        getContentPane().add(btnPromedi);
        btnPromedi.setBounds(10, 260, 100, 30);
        getContentPane().add(txtPromedio);
        txtPromedio.setBounds(130, 260, 150, 30);

        tablaLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Cedula", "Nombre", "Apellido", "Notas finales"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaLista);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 70, 400, 170);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-429)/2, (screenSize.height-338)/2, 429, 338);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        int tamano = combo.getSelectedIndex();
        if (combo.getSelectedIndex() == 0) {
            try {
                // limpiarTabla();
                ordenado(tamano);
            } catch (IOException ex) {
                Logger.getLogger(ListadoPromedio.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        if (combo.getSelectedIndex() == 1) {
            try {
                limpiarTabla();
                ordenado(tamano);
            } catch (IOException ex) {
                Logger.getLogger(ListadoPromedio.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
        if (combo.getSelectedIndex() == 2) {
            try {
                limpiarTabla();
                ordenado(tamano);
            } catch (IOException ex) {
                Logger.getLogger(ListadoPromedio.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
        if (combo.getSelectedIndex() == 3) {
            try {
                limpiarTabla();
                ordenado(tamano);
            } catch (IOException ex) {
                Logger.getLogger(ListadoPromedio.class.getName()).log(Level.SEVERE, null, ex);
            }


        }

    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnPromediActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPromediActionPerformed
        // TODO add your handling code here:
        txtPromedio.setText(String.valueOf(Menu.getSec().getPromedioSeccion()));

    }//GEN-LAST:event_btnPromediActionPerformed

    private void comboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboActionPerformed
    void limpiarTabla() {
        
        for (int i = 0; i < tablaLista.getRowCount(); i++) {
            tablaLista.setValueAt("", i, 0);
            tablaLista.setValueAt("", i, 1);
            tablaLista.setValueAt("", i, 2);
            tablaLista.setValueAt("", i, 3);
        }




    }

    void ordenado(int s) throws IOException {
        int cantidad = Menu.getSec().getEstudiantes().size();
        switch (s) {
            case 0: {
                limpiarTabla();
                Menu.getSec().ordenarPorCedula();
                for (int i = 0; i < cantidad; i++) {
                    Collections.sort(Menu.getSec().getEstudiantes(), new ComparadorCedula());
                    int j = 0;
                    tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getCedula(), i, j);
                    tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getNombre(), i, j + 1);
                    tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getApellido(), i, j + 2);
                    tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getNotafinal(), i, j + 3);
                }
                break;
            }

            case 1: {

                limpiarTabla();
                Menu.getSec().ordenarPorNotaFinal();
                for (int i = 0; i < cantidad; i++) {
                    Collections.sort(Menu.getSec().getEstudiantes(), new ComparadorNotaFinal());
                    int j = 0;
                    tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getCedula(), i, j);
                    tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getNombre(), i, j + 1);
                    tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getApellido(), i, j + 2);
                    tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getNotafinal(), i, j + 3);
                }
                break;
            }
            case 2: {
                limpiarTabla();
                Menu.getSec().ListadoAprobado();
                for (int i = 0; i < cantidad; i++) {
                    int j = 0;
                    if (Menu.getSec().getEstudiantes().elementAt(i).getNotafinal() >= 10) {
                        tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getCedula(), i, j);
                        tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getNombre(), i, j + 1);
                        tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getApellido(), i, j + 2);
                        tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getNotafinal(), i, j + 3);
                    }
                }
                break;
            }
            case 3: {
                limpiarTabla();
                Menu.getSec().ListadoReprobado();
                for (int i = 0; i < cantidad; i++) {
                    int j = 0;
                    if (Menu.getSec().getEstudiantes().elementAt(i).getNotafinal() < 10) {
                        tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getCedula(), i, j);
                        tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getNombre(), i, j + 1);
                        tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getApellido(), i, j + 2);
                        tablaLista.setValueAt(Menu.getSec().getEstudiante(i).getNotafinal(), i, j + 3);
                    }
                }
                break;
            }
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ListadoPromedio().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnPromedi;
    private javax.swing.JComboBox combo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaLista;
    private javax.swing.JTextField txtPromedio;
    // End of variables declaration//GEN-END:variables
}
